$(function () {
  const login_state = "LOGIN_STATE";
  let state = getCookie(login_state);
  if (!(state && state == "OK")) {
    history.back();
    return;
  }
  let user = localStorage.getItem("user");
  if (!user) {
    clearCookie(login_state);
    location.reload();
  }
  user = JSON.parse(user);

  let status = ["未支付", "已支付", "待发货", "邮递中", "待收货", "已完成"];
  let ddzt = status[0];
  let sta = decodeURIComponent($.getParam("status"));
  if (sta && status.includes(sta)) {
    ddzt = sta;
  }
  $(".title-info").text("我的" + ddzt + "订单");
  $.pajax({
    url: "/api/myorder.php",
    type: "get",
    dataType: "json",
    data: {
      user_id: user.user_id,
      status: ddzt,
    },
  }).then((res) => {
    if (res.code == 1) {
      $(".order").html(renderOrder(res.data));
      let totalMoney = res.data.reduce((pre, item) => {
        return pre + item.total_money * 1;
      }, 0);
      $(".totalMoney").html("&yen;" + totalMoney);
      if (res.data.length == 0) {
        $(".order").html("您暂没有" + ddzt + "的订单");
        return;
      }
      $(".gopay").show();
      if (sta == status[1]) {
        $(".gopay").hide();
      }
    }
  });

  let order_no_list = [];
  function renderOrder(data) {
    return data
      .map((item) => {
        order_no_list.push(`'${item.order_no}'`);
        return `<div class="order-item" order_id="${item.order_id}">
            <div class="order-info">
                <div class="oi-header"><span>订单号：${
                  item.order_no
                }</span><span>${item.add_time}</span></div> 
                <div class="name-tel"><span>收件人：${
                  item.sjr
                }</span> <span>电话：${item.tel}</span></div>
                <div class="address"><p>收件地址：</p> <p>${
                  item.sjdz
                }</p></div> 
            </div>
            <div class="order-detail">
                ${renderDetailOrder(item.detail)}
            </div>
        </div>`;
      })
      .join("");
  }

  function renderDetailOrder(data) {
    return data
      .map((item) => {
        return `<div class="detail-item">
            <img src="${item.img}" alt="" class="goods_img" goods_id="${item.goods_id}">
            <div class="item">
                <div class="item-name">${item.goods_name}</div>
                <div class="item-pn"><span class="price">&yen;${item.goods_new_price}</span><span class="num">x ${item.cart_num}</span><span class="total price">小计：&yen;${item.total}</span></div>
            </div>
        </div>`;
      })
      .join("");
  }

  $(document).on("click", ".goods_img", function () {
    let goods_id = $(this).attr("goods_id");
    console.log(goods_id);
    location.href = "../html/detail.html?goods_id=" + goods_id;
  });

  $(".gopay").click(() => {
    if (order_no_list.length == 0) return;
    console.log(user.user_id);
    console.log(order_no_list);
    $.pajax({
      url: "/api/myorder.php",
      type: "post",
      dataType: "json",
      data: {
        type: "payment",
        user_id: user.user_id,
        order_no: order_no_list.join(","),
      },
    }).then((res) => {
      new Model({
        title: "信息",
        content: res.code,
        showCancel: false,
        skin: "tan" /* 主题色 "#20222A"|"#1E9FFF"|"tan" */,
        confirmCallbak: function () {
          this.close();
        },
      }).init();
      if (res.code == 1) {
        location.href = "../html/myorder.html?status=已支付";
      }
    });
  });
});
